package org.jeecg.modules.demo.weixin.smtdev.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.api.UpmsHelloApi;
import org.jeecg.modules.demo.entity.Dept;
import org.jeecg.modules.demo.mall.api.MallHelloApi;
import org.jeecg.modules.demo.mall.entity.YxUser;
import org.jeecg.modules.demo.weixin.entity.SmtDev;
import org.jeecg.modules.demo.weixin.smtdev.service.ISmtDevService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: smt_dev
 * @Author: jeecg-boot
 * @Date: 2023-12-21
 * @Version: V1.0
 */
@Api(tags = "smt_dev")
@RestController
@RequestMapping("/weixin/smtdev/smtDev")
@Slf4j
public class SmtDevController extends JeecgController<SmtDev, ISmtDevService> {
    @Autowired
    private ISmtDevService smtDevService;
    @Autowired
    private MallHelloApi mallHelloApi;
    @Autowired
    private UpmsHelloApi upmsHelloApi;

    /**
     * 分页列表查询
     *
     * @param smtDev
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "smt_dev-分页列表查询")
    @ApiOperation(value = "smt_dev-分页列表查询", notes = "smt_dev-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SmtDev>> queryPageList(SmtDev smtDev,
                                               @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                               HttpServletRequest req) {
        QueryWrapper<SmtDev> queryWrapper = QueryGenerator.initQueryWrapper(smtDev, req.getParameterMap());
        Page<SmtDev> page = new Page<SmtDev>(pageNo, pageSize);
        IPage<SmtDev> pageList = smtDevService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param smtDev
     * @return
     */
//    @AutoLog(value = "smt_dev-添加")
    @ApiOperation(value = "smt_dev-添加", notes = "smt_dev-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SmtDev smtDev) {
        smtDevService.save(smtDev);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param smtDev
     * @return
     */
//    @AutoLog(value = "smt_dev-编辑")
    @ApiOperation(value = "smt_dev-编辑", notes = "smt_dev-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SmtDev smtDev) {
        smtDevService.updateById(smtDev);
        return Result.OK("操作成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
//    @AutoLog(value = "smt_dev-通过id删除")
    @ApiOperation(value = "smt_dev-通过id删除", notes = "smt_dev-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        smtDevService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
//    @AutoLog(value = "smt_dev-批量删除")
    @ApiOperation(value = "smt_dev-批量删除", notes = "smt_dev-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.smtDevService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "smt_dev-通过id查询")
    @ApiOperation(value = "smt_dev-通过id查询", notes = "smt_dev-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SmtDev> queryById(@RequestParam(name = "id", required = true) String id) {
        SmtDev smtDev = smtDevService.getById(id);
        if (smtDev == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(smtDev);
    }


    /**
     * 通过id查询
     *
     * @param userId
     * @return
     */
    //@AutoLog(value = "smt_dev-通过id查询")
    @ApiOperation(value = "smt_dev-通过id查询", notes = "smt_dev-通过id查询")
    @GetMapping(value = "/queryDev")
    public List<SmtDev> queryDev(@RequestParam(name = "userId", required = true) Integer userId) {
        if (userId == null) {
            return null;
        }
        LambdaQueryWrapper<SmtDev> smtDevLambdaQueryWrapper = new LambdaQueryWrapper<>();
        smtDevLambdaQueryWrapper.eq(SmtDev::getUserId, userId);
        List<SmtDev> devServiceOne = smtDevService.list(smtDevLambdaQueryWrapper);
        if (CollectionUtils.isEmpty(devServiceOne)) {
            return null;
        }
        return devServiceOne;
    }

    /**
     * 通过id查询
     *
     * @param userId
     * @return
     */
    //@AutoLog(value = "smt_dev-通过id查询")
    @ApiOperation(value = "smt_dev-通过id查询", notes = "smt_dev-通过id查询")
    @GetMapping(value = "/updateDev")
    public void updateDev(@RequestParam(name = "userId", required = true) Integer userId, @RequestParam(name = "merId", required = true) String merId) {
        if (userId != null) {
            LambdaQueryWrapper<SmtDev> smtDevLambdaQueryWrapper = new LambdaQueryWrapper<>();
            smtDevLambdaQueryWrapper.eq(SmtDev::getUserId, userId);
            SmtDev devServiceOne = smtDevService.getOne(smtDevLambdaQueryWrapper);
            if (devServiceOne != null) {
                devServiceOne.setMerchantId(merId);
                smtDevService.updateById(devServiceOne);
            }
        }
    }


    /**
     * 导出excel
     *
     * @param request
     * @param smtDev
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SmtDev smtDev) {
        return super.exportXls(request, smtDev, SmtDev.class, "smt_dev");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("smtdev:smt_dev:importExcel")
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SmtDev.class);
    }


    /**
     * 分页列表查询
     *
     * @param smtDev
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "smt_dev-分页列表查询")
    @ApiOperation(value = "smt_dev-分页列表查询", notes = "smt_dev-分页列表查询")
    @GetMapping(value = "/queryDevList")
    public Result<IPage<SmtDev>> queryDevList(SmtDev smtDev,
                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                              HttpServletRequest req) {
        QueryWrapper<SmtDev> queryWrapper = QueryGenerator.initQueryWrapper(smtDev, req.getParameterMap());
        Page<SmtDev> page = new Page<SmtDev>(pageNo, pageSize);
        IPage<SmtDev> pageList = smtDevService.page(page, queryWrapper);
        if (CollectionUtils.isEmpty(pageList.getRecords())) {
            return Result.OK(pageList);
        }
        pageList.getRecords().forEach(smtDev1 -> {
            if (smtDev1.getUserId() != null && smtDev1.getUserId() != 0 ) {
                YxUser yxUser = mallHelloApi.queryUser(smtDev.getUserId());
                if (yxUser != null) {
                    smtDev.setNickName(yxUser.getNickname());
                    smtDev.setPhone(yxUser.getPhone());
                }
            }
            if(StringUtils.isNotBlank(smtDev1.getMerchantId())){
                Dept dept = upmsHelloApi.queryByDept(Long.valueOf(smtDev1.getMerchantId()));
                if (dept!=null){
                    smtDev1.setMerName(dept.getName());
                }
            }
        });
        return Result.OK(pageList);
    }


    /**
     * 根据门店查看设备
     *
     * @param merId
     * @return
     */
    @ApiOperation(value = "smt_dev-分页列表查询", notes = "smt_dev-分页列表查询")
    @GetMapping(value = "/queryMer")
    public Result<List<SmtDev>> queryMer(@RequestParam String merId ,  String devCode) {
        QueryWrapper<SmtDev> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(devCode)){
            queryWrapper.like("dev_code", devCode);
        }
        queryWrapper.eq("merchant_id", merId);
        List<SmtDev> smtDevList = smtDevService.list(queryWrapper);
        if (CollectionUtils.isEmpty(smtDevList)) {
            return Result.ok(smtDevList);
        }
        smtDevList.forEach(smtDev -> {
            if (smtDev.getUserId() != null && smtDev.getUserId() != 0) {
                YxUser yxUser = mallHelloApi.queryUser(smtDev.getUserId());
                if (yxUser != null) {
                    smtDev.setNickName(yxUser.getNickname());
                    smtDev.setPhone(yxUser.getPhone());
                }
            }
        });
        return Result.OK(smtDevList);

    }


    /**
     * 修改设备绑定门店
     *
     * @param smtDev
     * @return
     */
    @ApiOperation(value = "修改设备绑定门店", notes = "smt_dev-修改设备绑定门店")
    @PostMapping(value = "/updateMerCode")
    public Result<?> updateMerCode(@RequestBody SmtDev smtDev) {
        if (StringUtils.isNotBlank(smtDev.getDevIds())) {
            List<Integer> integerList = Arrays.stream(smtDev.getDevIds().split(",")).map(s -> Integer.valueOf(s)).collect(Collectors.toList());
            UpdateWrapper<SmtDev> updateWrapper = new UpdateWrapper();
            updateWrapper.in("id", integerList);
            updateWrapper.set("merchant_id", smtDev.getMerchantId());
            smtDevService.update(updateWrapper);
        }
        return Result.OK();
    }

}
